﻿Imports TMySQL
Public Class frmIndex
    Private _Indexes As TMyIndexes
    Private _Table As IHasColumns
    Private Enum TCol As Integer
        coName = 0
        coUnique = 1
        coCollation = 1
        coIndexType = 2
        coCardinality = 3
        coSubPart = 4
        coPacked = 5
        coNullable = 6
        coComment = 7
        coIndexComment = 8
    End Enum

    Public Sub New(ByVal ATable As IHasColumns)
        Me.New()
        _Table = ATable
        _Indexes = New TMyIndexes(ATable)
        InitFrm()
    End Sub
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub

    Private Sub InitTree()
        tv.Nodes.Clear()
        Dim nodeP As TreeNode = tv.Nodes.Add("PRIMARY")
        Dim nodeU As TreeNode = tv.Nodes.Add("UNIQUE")
        Dim nodeB As TreeNode = tv.Nodes.Add("BTREE")
        Dim dic As New Dictionary(Of String, TreeNode)
        Dim node As TreeNode

        For i As Integer = 0 To _Indexes.EtcIndexTypeList.Count - 1
            dic.Add(_Indexes.EtcIndexTypeList(i), tv.Nodes.Add(_Indexes.EtcIndexTypeList(i)))
        Next
        For i As Integer = 0 To _Indexes.Count - 1
            If _Indexes(i).IsPrimaryKey Then
                node = nodeP.Nodes.Add(_Indexes(i).IndexName)
            ElseIf _Indexes(i).IsUnique Then
                node = nodeU.Nodes.Add(_Indexes(i).IndexName)
            ElseIf _Indexes(i).IsBTree Then
                node = nodeB.Nodes.Add(_Indexes(i).IndexName)
            Else
                node = dic(_Indexes(i).IndexType).Nodes.Add(_Indexes(i).IndexName)
            End If
            node.Tag = _Indexes(i)
        Next
        tv.ExpandAll()
    End Sub

    Private Sub InitFrm()
        Grid.Rows.Clear()
        lvColumns.Items.Clear()
        tv.Nodes.Clear()
        frmMain.GeneralSettings.ApplyAll(Me)
        frmMain.GridSettings.ApplyAll(Me)
        frmMain.EditorSettings.ApplyAll(Me)
        _Indexes.Refresh()
        InitTree()
    End Sub

    Private Sub tv_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tv.AfterSelect
        If tv.SelectedNode.Level <> 1 Then
            Return
        End If
        If tv.SelectedNode.Tag Is Nothing Then
            Return
        End If
        Cursor.Current = Cursors.WaitCursor
        Try
            ShowIndex(DirectCast(tv.SelectedNode.Tag, TMyIndex))
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub ShowIndex(ByVal AIndex As TMyIndex)
        lvColumns.Items.Clear()
        For i As Integer = 0 To AIndex.ColumnCount - 1
            Dim li As New ListViewItem
            li.Text = AIndex.Columns(i).ColumnName
            li.SubItems.Add(AIndex.Columns(i).Collation)
            li.SubItems.Add(AIndex.Columns(i).Nullable)
            lvColumns.Items.Add(li)
        Next
        For i As Integer = 0 To lvColumns.Columns.Count - 1
            lvColumns.Columns(i).Width = -2
        Next
        Grid.Rows.Clear()

        Grid.Rows.Add("名前", AIndex.IndexName)
        Grid.Rows.Add("Cardinality", AIndex.Cardinality)
        Grid.Rows.Add("SubPart", AIndex.SubPart)
        Grid.Rows.Add("Packed", AIndex.Packed)
        Grid.Rows.Add("IndexType", AIndex.IndexType)
        Grid.Rows.Add("コメント", AIndex.Comment)
        Grid.Rows.Add("インデックスコメント", AIndex.IndexComment)

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        If tv.SelectedNode.Level <> 1 Then
            Return
        End If
        If tv.SelectedNode.Tag Is Nothing Then
            Return
        End If
        If MsgBox("インデックス " & tv.SelectedNode.Text & " を削除します。" & vbNewLine & "よろしいですか？", MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Cursor.Current = Cursors.WaitCursor
            Try
                DropIndex(DirectCast(tv.SelectedNode.Tag, TMyIndex))
                Call MsgBox("削除しました。", MsgBoxStyle.Information)
                InitFrm()
            Catch ex As Exception
            Finally
            End Try
        End If

    End Sub

    Private Sub DropIndex(ByVal AIndex As TMyIndex)
        Dim myQuery As New TMyQuery
        Try
            myQuery.Session = frmMain.Connection
            myQuery.SQL.Append("ALTER TABLE `").Append(AIndex.TableSchema).Append("`.`").Append(AIndex.TableName).AppendLine("`")
            If AIndex.IsPrimaryKey Then
                myQuery.SQL.AppendLine("DROP PRIMARY KEY")
            Else
                myQuery.SQL.Append("DROP INDEX ").AppendLine(AIndex.IndexName)
            End If
            myQuery.Execute()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            myQuery.Dispose()
        End Try
    End Sub

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        Dim frmRet As New frmCreateIndex(_Table)
        Try
            If frmRet.ShowDialog() = Windows.Forms.DialogResult.OK Then
                InitFrm()
            End If
        Finally
            frmRet.Dispose()
        End Try
    End Sub
End Class